package com.study.concurrentprogramming.option;

import java.util.Objects;
import java.util.Optional;

/**
 * @Description : 测试Optional工具类
 * @Author : guxuemin
 * @Version : V1.0.0
 * @Date : 2021/8/1 10:20
 */
public class OptionalTest {
    public Optional<String> getOptionResult(String user) {
        if (Objects.nonNull(user)) {
            return Optional.ofNullable(user);
        }
        return Optional.empty();
    }


    public String getResult() {
        System.out.println("call result");
        return "result";
    }

    public void testOrElseGet() {
        // out:
        Optional.ofNullable("result").orElseGet(() -> {
            System.out.println("orElseGet call exe");
            return "call";
        });
        // out: call result
        Optional.ofNullable("result").orElse(getResult());
    }

    public static void main(String[] args) {
        OptionalTest optionalTest = new OptionalTest();
        optionalTest.testOrElseGet();
    }

    public User testThrow(User user) throws Exception {
        return Optional.ofNullable(user).orElseThrow(Exception::new);
    }

    private class User{
    }

    public String test() {
        String status = getResult();
        return Optional.ofNullable(status).orElse("pending");

    }
}
